package com.ruoyi.sywx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.sywx.domain.YqPosterLink;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sywx.domain.bo.YqUnitLinkBo;
import com.ruoyi.sywx.domain.vo.YqUnitLinkVo;
import com.ruoyi.sywx.domain.YqUnitLink;
import com.ruoyi.sywx.mapper.YqUnitLinkMapper;
import com.ruoyi.sywx.service.IYqUnitLinkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 舆情和涉情单位关联Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@RequiredArgsConstructor
@Service
public class YqUnitLinkServiceImpl implements IYqUnitLinkService {

    private final YqUnitLinkMapper baseMapper;

    /**
     * 查询舆情和涉情单位关联
     */
    @Override
    public YqUnitLinkVo queryById(Long yqId){
        return baseMapper.selectVoById(yqId);
    }

    /**
     * 查询舆情和涉情单位关联列表
     */
    @Override
    public TableDataInfo<YqUnitLinkVo> queryPageList(YqUnitLinkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<YqUnitLink> lqw = buildQueryWrapper(bo);
        Page<YqUnitLinkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询舆情和涉情单位关联列表
     */
    @Override
    public List<YqUnitLinkVo> queryList(YqUnitLinkBo bo) {
        LambdaQueryWrapper<YqUnitLink> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<YqUnitLink> buildQueryWrapper(YqUnitLinkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<YqUnitLink> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getYqId() != null, YqUnitLink::getYqId, bo.getYqId());
        lqw.eq(bo.getUnitId() != null, YqUnitLink::getUnitId, bo.getUnitId());
        return lqw;
    }

    /**
     * 新增舆情和涉情单位关联
     */
    @Override
    public Boolean insertByBo(YqUnitLinkBo bo) {
        YqUnitLink add = BeanUtil.toBean(bo, YqUnitLink.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setYqId(add.getYqId());
        }
        return flag;
    }

    /**
     * 修改舆情和涉情单位关联
     */
    @Override
    public Boolean updateByBo(YqUnitLinkBo bo) {
        YqUnitLink update = BeanUtil.toBean(bo, YqUnitLink.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(YqUnitLink entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除舆情和涉情单位关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean insertBatch(List<YqUnitLink> yqUnitLinkList) {
        return baseMapper.insertBatch(yqUnitLinkList);
    }

    @Override
    public boolean deleteByYqId(Long YqId) {
        return baseMapper.delete(Wrappers.lambdaQuery(YqUnitLink.class).eq(YqUnitLink::getYqId, YqId)) > 0;
    }

}
